<template>
  <div class="yd-form register">
		<group gutter='0'>
			<x-input title="昵称：" v-model="form.user" placeholder="请输入您的昵称" ref="user" @on-change="inputing" required></x-input>
			<x-input title="登录密码：" v-model="form.pwd" placeholder="请输入您的登录密码" ref="pwd" @on-change="inputing" required :type="pwdType1 ? 'password' : 'text'" class="pwdeye">
        <x-button slot="right" type="primary" plain mini @click.native="pwdType1 = !pwdType1" :class="pwdType1 + ''">
					<span>eye</span>
				</x-button>
      </x-input>
      <x-input title="确认密码：" v-model="form.pwd2" placeholder="请再次输入您的登录密码" ref="pwd2" @on-change="inputing" required :type="pwdType2 ? 'password' : 'text'" class="pwdeye">
        <x-button slot="right" type="primary" plain mini @click.native="pwdType2 = !pwdType2" :class="pwdType2 + ''">
					<span>eye</span>
				</x-button>
      </x-input>
		</group>

		<x-button type="primary" :disabled="!valid" @click.native="finish" class="btn-login">完成并登录</x-button>

		<p class="agree f24">点击注册表示已同意<router-link to="/protocol">《一点高人使用协议》</router-link></p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        user: '',
        pwd: '',
        pwd2: '',
      },
      pwdType1: true,
      pwdType2: true,
      valid: false,
    };
  },
  methods: {
    inputing() {
      this.valid = this.$refs.user.valid && this.$refs.pwd.valid && this.$refs.pwd2.valid;
    },
    finish() {
      if(this.form.pwd != this.form.pwd2) {
        return this.$vux.toast.text('两次密码不一致<br/>请重新输入');
      }

      this.$http.fetchGet("registerByPhone", {
				phone: this.$route.query.phone,
				code: this.$route.query.code,
			}).then(({ data = {} }) => {
        console.log("注册", data);
        data.status == 1 && this.step2(data);
			});
    },
    step2({userId}) {
      this.$http.fetchGet("registerStep2", {
				userId: userId,
        userName: this.form.user,
        loginPwd: this.form.pwd,
			}).then(({ data }) => {
        console.log("注册2", data);
        if(data) {
          sessionStorage.phone = this.$route.query.phone;
          sessionStorage.pwd = this.form.pwd;
          this.$vux.toast.text("注册成功，自动返回登录中...");
          setTimeout(() => {
            this.$router.go(-2);
          }, 2000);
        }
			});
    }
  }
};
</script>

<style scoped lang="less">
.weui-cell_warn{
  color: #333333;
}
</style>
